//
//  WNLibRecView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/31.
//

import UIKit

class WNLibRecView: UIView {
    
    static let shared = WNLibRecView()
    
    lazy var bgView: UIView = {
        let view = UIView.init(frame: CGRect.zero)
        view.backgroundColor = .white
        view.backgroundColor = WNColor.Fade().top2Bottom(.init(width: 250, height: 355))
        view.wn_cornerWithBorder(10)
        return view
    }()
    
    lazy var bookImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "placeholder_cover_ver"))
        image.contentMode = .scaleAspectFill
        image.wn_cornerWithBorder(4)
        image.clipsToBounds = true
        return image
    }()

    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 16)
        label.textColor = .black
        label.numberOfLines = 0
        return label
    }()

    lazy var desLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 13)
        label.textColor = .init(named: "gray_999999")
        label.numberOfLines = 4
        return label
    }()

    lazy var readBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("READ NOW", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 14)
        btn.setImage(UIImage(named: "library_rec_arrow_white"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            hidenLibRec()
            wn_getTopVC()?.wn_pushBookVC(self?.model?.bookId ?? "")
        }.disposed(by: rx.disposeBag)
        btn.backgroundColor = WNColor.Fade().left2right(.init(width: 180, height: 36))
        btn.wn_cornerWithBorder(18)
        return btn
    }()
    
    lazy var closeBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setImage(UIImage(named: "lib_rec_close"), for: .normal)
        btn.rx.tap.bind { [weak self] in
            self?.recClosure?()
            hidenLibRec()
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    var recClosure: ReturnClosure?
    
    var model: BookDetailModel? {
        didSet {
            if let m = model {
                bookImage.wn_setupImg(m.coverUrl, placeHolder: NSLocalizedString("placeholder_cover_ver", comment: ""))
                nameLabel.text = m.name
                desLabel.text = m.des
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .init(white: 0, alpha: 0.55)
        addSubview(bgView)
        bgView.addSubview(bookImage)
        bgView.addSubview(nameLabel)
        bgView.addSubview(desLabel)
        bgView.addSubview(readBtn)
        addSubview(closeBtn)
        bgView.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(NaviTotalHeight+scaleHeight(120))
            make.width.equalTo(250)
            make.height.equalTo(355)
        }
        bookImage.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(20)
            make.size.equalTo(CGSize(width: 100, height: 144))
        }
        nameLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(bookImage.snp.bottom).offset(6)
        }
        desLabel.snp.makeConstraints { (make) in
            make.top.equalTo(nameLabel.snp.bottom).offset(6)
            make.left.equalTo(20)
            make.right.equalTo(-20)
        }
        readBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-20)
            make.size.equalTo(CGSize(width: 180, height: 36))
        }
        closeBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(bgView.snp.bottom).offset(26)
            make.size.equalTo(CGSize(width: 36, height: 36))
        }
        readBtn.layoutButton(style: .right, imageTitleSpace: 4)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

func showLibRec(_ vc: WNTabBarVC, _ model: BookDetailModel?) {
    WNLibRecView.shared.frame = .init(x: 0, y: 0, width: ScreenW, height: ScreenH)
    WNLibRecView.shared.model = model
    vc.view.addSubview(WNLibRecView.shared)
}

func hidenLibRec() {
    WNLibRecView.shared.removeFromSuperview()
}
